package com.ycy.temperature.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;

import com.airsaid.pickerviewlibrary.CityPickerView;
import com.airsaid.pickerviewlibrary.OptionsPickerView;
import com.airsaid.pickerviewlibrary.listener.OnSimpleCitySelectListener;
import com.jude.utils.JUtils;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.ycy.temperature.R;
import com.ycy.temperature.bean.VerificationBean;
import com.ycy.temperature.date.net.UserNetWorks;
import com.ycy.temperature.date.utils.CheckUtils;
import com.ycy.temperature.view.base.BaseActivity;
import com.ycy.temperature.view.view.TitleView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;

/**
 * Created by MI on 2018/7/17.
 */

public class InformationActivity extends BaseActivity {
    @BindView(R.id.title)
    TitleView title;
    @BindView(R.id.Text_Age)
    TextView TextAge;
    @BindView(R.id.Text_NativePlace)
    TextView TextNativePlace;
    @BindView(R.id.Text_City)
    TextView TextCity;
    @BindView(R.id.Text_Marriage)
    TextView TextMarriage;
    @BindView(R.id.Text_Children)
    TextView TextChildren;
    @BindView(R.id.Text_Height)
    AutoCompleteTextView TextHeight;
    @BindView(R.id.Text_Interest)
    TextView TextInterest;
    @BindView(R.id.Text_Income)
    AutoCompleteTextView TextIncome;
    @BindView(R.id.Text_Weight)
    AutoCompleteTextView TextWeight;
    @BindView(R.id.Text_Smoke)
    TextView TextSmoke;
    @BindView(R.id.Text_Drink)
    TextView TextDrink;
    @BindView(R.id.Text_Zodiac)
    TextView TextZodiac;
    @BindView(R.id.Text_Religion)
    TextView TextReligion;
    private boolean isLoaded = false;
    private static final int MSG_LOAD_DATA = 0x0001;
    private static final int MSG_LOAD_SUCCESS = 0x0002;
    private static final int MSG_LOAD_FAILED = 0x0003;
    private ArrayList<ArrayList<String>> options2Items = new ArrayList<>();
    private ArrayList<ArrayList<ArrayList<String>>> options3Items = new ArrayList<>();
    private Thread thread;
    private ArrayList<String> MarriageList = new ArrayList<String>();
    private ArrayList<String> ChildrenList = new ArrayList<String>();
    private ArrayList<String> CurrencyList = new ArrayList<String>();
    private ArrayList<String> ZodiacyList = new ArrayList<String>();

    private OptionsPickerView<String> mOptionsPickerView;
    private CityPickerView mCityPickerView;

    Intent intent = null;
    String mobile = "";
    String password = "";

    @Override
    protected int getLayout() {
        return R.layout.activity_information;
    }

    @Override
    protected void setData() {

    }


    @Override
    protected void init(Bundle inBundle) {
        ButterKnife.bind(this);
        QMUIStatusBarHelper.setStatusBarLightMode(this);
        title.getLeft_ibtn().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        title.getRight_tv().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!getVerification()) return;
                getInformation();
            }
        });

        intent = getIntent();
        mobile = intent.getStringExtra("mobile");
        password = intent.getStringExtra("password");

        mCityPickerView = new CityPickerView(this);
        mOptionsPickerView = new OptionsPickerView<>(this);

        MarriageList.add("未婚");
        MarriageList.add("已婚");
        ChildrenList.add("有");
        ChildrenList.add("无");
        CurrencyList.add("是");
        CurrencyList.add("否");
        ZodiacyList.add("鼠");
        ZodiacyList.add("牛");
        ZodiacyList.add("虎");
        ZodiacyList.add("兔");
        ZodiacyList.add("龙");
        ZodiacyList.add("蛇");
        ZodiacyList.add("马");
        ZodiacyList.add("羊");
        ZodiacyList.add("猴");
        ZodiacyList.add("鸡");
        ZodiacyList.add("狗");
        ZodiacyList.add("猪");
    }

    @OnClick({R.id.Text_City, R.id.Text_Marriage, R.id.Text_Children, R.id.Text_Smoke, R.id.Text_Drink, R.id.Text_Zodiac})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.Text_City:
                showPickerView();
                break;
            case R.id.Text_Marriage:
                selectOption(MarriageList, TextMarriage);
                break;
            case R.id.Text_Children:
                selectOption(ChildrenList, TextChildren);
                break;
            case R.id.Text_Smoke:
                selectOption(CurrencyList, TextSmoke);
                break;
            case R.id.Text_Drink:
                selectOption(CurrencyList, TextDrink);
                break;
            case R.id.Text_Zodiac:
                selectOption(ZodiacyList, TextZodiac);
                break;
        }
    }

    public void selectOption(final ArrayList<String> list, final TextView textView) {
        mOptionsPickerView = new OptionsPickerView<>(this);
        // 设置数据
        mOptionsPickerView.setPicker(list);
        // 设置选项单位
        mOptionsPickerView.setOnOptionsSelectListener(new com.airsaid.pickerviewlibrary.OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int option1, int option2, int option3) {
                String sex = list.get(option1);
                textView.setText(sex);
            }
        });
        mOptionsPickerView.show();
    }

    private void showPickerView() {// 弹出选择器

        mCityPickerView.setOnCitySelectListener(new OnSimpleCitySelectListener() {
            @Override
            public void onCitySelect(String prov, String city, String area) {
                // 省、市、区 分开获取
                TextCity.setText(prov + "-" + city + "-" + area);
            }

            @Override
            public void onCitySelect(String str) {
                // 一起获取

            }
        });
        mCityPickerView.show();
    }

    public void getInformation() {
        UserNetWorks.getInformation(mobile, password, TextAge.getText().toString(), TextNativePlace.getText().toString(), TextCity.getText().toString(), TextMarriage.getText().toString(), TextChildren.getText().toString(), TextHeight.getText().toString(), TextInterest.getText().toString(), TextIncome.getText().toString(), TextWeight.getText().toString(), TextSmoke.getText().toString(), TextDrink.getText().toString(), TextZodiac.getText().toString(), TextReligion.getText().toString(), new Subscriber<VerificationBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                JUtils.Toast("网络错误");
            }

            @Override
            public void onNext(VerificationBean verificationBean) {
                if (verificationBean.getResponseState().equals("1")) {
                    JUtils.Toast(verificationBean.getMsg());
                    // finish();
                } else {
                    JUtils.Toast(verificationBean.getMsg());
                }
            }
        });
    }

    public boolean getVerification() {
        if (CheckUtils.isEmpty(TextCity.getText().toString())) {
            JUtils.Toast("请输入所在城市");
            return false;
        }
        if (CheckUtils.isEmpty(TextMarriage.getText().toString())) {
            JUtils.Toast("请输入婚姻状况");
            return false;
        }
        if (CheckUtils.isEmpty(TextChildren.getText().toString())) {
            JUtils.Toast("请输入是否有子女");
            return false;
        }
        if (CheckUtils.isEmpty(TextHeight.getText().toString())) {
            JUtils.Toast("请输入身高");
            return false;
        }
        if (CheckUtils.isEmpty(TextInterest.getText().toString())) {
            JUtils.Toast("请输入兴趣爱好");
            return false;
        }
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (mCityPickerView.isShowing()) {
                mCityPickerView.dismiss();
                return true;
            }

            if (mOptionsPickerView.isShowing()) {
                mOptionsPickerView.dismiss();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
